<?php
/**
 * 表数据模型
 */
class core_m_user extends core_base_m {
	
	public function primarykey() {
		return 'uid';
	}
	public function tableName() {
		return core_base_Constant::TABLE_PREFIX . 'user';
	}
	/**
	 * 登录
	 * @param $loginArr 登录需要的数据
	 */
	public function login($loginArr){
		//用户名登录
		if($loginArr['username'] and $loginArr['password']){
			$pwd = md5($loginArr['password']);
			$condi = "username='{$loginArr['username']}' and password='{$pwd}'";
			$errorMsg = "用户名或者密码错误";
		}
		//邮箱登录
		if($loginArr['email'] and $loginArr['password']){
			$pwd = md5($loginArr['password']);
			$condi = "email='{$loginArr['email']}' and password='{$pwd}'";
			$errorMsg = "邮箱或者密码错误";
		}
		//第三方登录
		if($loginArr['appkey']){
			$condi = "appkey='{$loginArr['appkey']}'";
			$errorMsg = "未授权的第三方登录";
		}
		if(!$condi) return false;
		$rs = $this->get($condi);
		if($rs){
			if($rs['islock']==1){
				$this->setError(0,'帐号被锁定。');
				return false;
			}
			$cookie ['uid'] = $rs ['uid'];
			$cookie ['username'] = $rs ['username'];
			$cookie ['logintime'] = $this->_time;
			$cookie ['key'] = md5 ( $rs ['uid'] . $rs ['username'] . $this->_time . core_base_Constant::COOKIE_KEY );
			$mNotice = core_base_mAPI::get('core_m_notice');
			$noticeRs = $mNotice->selectOne("t_userid={$rs['uid']} and isread=0","count(1) as num");
			$cookie ['msgcount'] = $noticeRs['num'];
			SUtil::ssetcookie ( $cookie, $loginArr['timeout'] );
			$this->set('logintime', $this->_time);
			$this->set('loginip', SUtil::getIp());
			$this->save($rs['uid']);
			return $rs;
		}else{
			$this->setError(0,$errorMsg);
			return false;
		}
	}
	/**
	 * 注册
	 * @param $userArr 主表数据
	 * @param $infoArr info表数据
	 * @return 自增ID
	 */
	public function reg($userArr,$infoArr=array()){
		foreach ($userArr as $k=>$v){
			$this->set($k, $v);
		}
		$this->set('regtime', $this->_time);
		$this->set('regymd', date('Y-m-d',$this->_time));
		$this->set('logintime', $this->_time);
		$ip = SUtil::getIP();
		$this->set('regip', $ip);
		$this->set('loginip', $ip);
		$uid = $this->save();
		if($uid){
			$infoArr['uid'] = $uid;
			$infoArr['username'] = $userArr['username'];
			$mUserInfo = core_base_mAPI::get ( "core_m_userinfo" );
			if($mUserInfo->insert($infoArr)===false){
				$this->delete("uid={$uid}");
				$this->setError(0,"插入info表失败。".$this->getDbError());
				return false;
			}else{
				return $uid;
			}
		}else{
			$this->setError(0,"插入主表失败。".$this->getDbError());
			return false;
		}
	}
	/**
	 * 编辑用户资料 new core_m_user(1)
	 * @param $userArr 主表数据
	 * @param $infoArr info表数据
	 * @return Boolean
	 */
	public function edit($userArr,$infoArr=''){
		foreach ($userArr as $k=>$v){
			$this->set($k, $v);
		}
		if($this->save()===false){
			$this->setError(0,"编辑主表失败。".$this->getDbError());
			return false;
		}
		if(is_array($infoArr)){
			$mUserInfo = core_base_mAPI::get ( "core_m_userinfo",$this->getData('uid') );
			foreach ($infoArr as $k=>$v){
				$mUserInfo->set($k, $v);
			}
			if($mUserInfo->save()===false){
				$this->setError(0,"编辑info表失败。".$this->getDbError());
				return false;
			}
		}
		return true;
	}
}